package application;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.security.PublicKey;
import java.util.Scanner;

import javafx.beans.property.SimpleDoubleProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.media.AudioClip;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class Settings {
	

	AudioClip mouseEnterAudio;  
	AudioClip mouseClickAudio; 
	
	double scale;
	
	String windowSizeString;
	boolean isWindowSizeChanged;
	
	Slider volume;
	
	
	public Settings() {
		this.mouseClickAudio = new AudioClip(new File("auds/MouseEnter.mp3").toURI().toString());
		this.mouseEnterAudio = new AudioClip(new File("auds/MouseClick.mp3").toURI().toString());
		this.scale = BasicTools.scale.getValue();
		isWindowSizeChanged = false;
	}
	
	
	public  Scene getSettingsScene(Stage primaryStage) {
		// TODO Auto-generated method stub	
		Scene scene = new Scene(getSettingsPane(primaryStage));
		mouseEnterAudio.volumeProperty().bind(BasicTools.volumeValue);
		mouseClickAudio.volumeProperty().bind(BasicTools.volumeValue);
		return scene;
	}
	
	
	public Scene getSettingsScene(Stage primaryStage, Scene backScene) {
		Scene scene = new Scene(getSettingsPane(primaryStage, backScene));
		mouseEnterAudio.volumeProperty().bind(BasicTools.volumeValue);
		mouseClickAudio.volumeProperty().bind(BasicTools.volumeValue);
		return scene;
	}
	
	public VBox getSettingsPane(Stage primaryStage) {
		VBox vBox = new VBox(30 * scale);
		vBox.setAlignment(Pos.BOTTOM_RIGHT);
		vBox.setStyle("-fx-background-image: url(file:pics/SettingsBackground.png); -fx-background-size: " + 
					BasicTools.windowSize[0] + " " + BasicTools.windowSize[1]);
		vBox.setPadding(new Insets(0, 0, 40 * scale, 0));
		vBox.getChildren().add(getResolutionPane());
		vBox.getChildren().add(getVolumnPane());
		vBox.getChildren().add(getBackSavePane(primaryStage));
		return vBox;
	}
	
	public VBox getSettingsPane(Stage primaryStage, Scene scene) {
		VBox vBox = new VBox(30 * scale);
		vBox.setAlignment(Pos.BOTTOM_RIGHT);
		vBox.setStyle("-fx-background-image: url(file:pics/SettingsBackground.png); -fx-background-size: " + 
					BasicTools.windowSize[0] + " " + BasicTools.windowSize[1]);
		vBox.setPadding(new Insets(0, 0, 40 * scale, 0));
		
		
		vBox.getChildren().add(getResolutionPane());
		vBox.getChildren().add(getVolumnPane());
		vBox.getChildren().add(getBackSavePane(primaryStage, scene));
		return vBox;
	}
	
	

	public  HBox getResolutionPane() {
		HBox resolutionPane = new HBox((60 + 5)  * scale);
		resolutionPane.setPadding(new Insets(0, (100 + 25)  * scale, 0 ,0));
		
		ComboBox<String> resolution = new ComboBox<>();
		resolution.setPrefWidth(120  * scale);
		resolution.setPrefHeight(20  * scale);
		resolution.setStyle("-fx-font-weight: bold; -fx-font-size: " + 15 * scale);
		String[] optionsString = {"Large", "Medium", "Small"};
		ObservableList<String> optionsObservableList = FXCollections.observableArrayList(optionsString); 
		resolution.getItems().addAll(optionsObservableList);
		
		File file = new File("Settings.txt");
		
		try (Scanner scanner = new Scanner(file)) {
			resolution.setValue(scanner.nextLine());
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		windowSizeString = resolution.getValue();
		
		resolution.setOnAction(e ->{
			
			if (resolution.getValue().equals("Large")) {
				if (! windowSizeString.equals("Large")) {
					isWindowSizeChanged = true;
					windowSizeString = "Large";
				}
			}
			else if (resolution.getValue().equals("Medium")) {

				if (! windowSizeString.equals("Medium")) {
					isWindowSizeChanged = true;
					windowSizeString = "Medium";
				}
			}
			else if (resolution.getValue().equals("Small")) {
			
				if (! windowSizeString.equals("Small")) {
					isWindowSizeChanged = true;
					windowSizeString = "Small";
				}
			}
		});
		
		
		ImageView resolutionImageView = new ImageView("pics/ResolutionLabel.png");
		resolutionImageView.setFitWidth(67 * 4  * scale);
		resolutionImageView.setFitHeight(27 * 4  * scale);
		
		
		
		Label resolutionLabel = new Label("", resolutionImageView);
		
		
		resolutionPane.setAlignment(Pos.CENTER_RIGHT);
		resolutionPane.getChildren().add(resolutionLabel);
		resolutionPane.getChildren().add(resolution);
		
		//resolutionPane.setStyle("-fx-background-color:red");
		return resolutionPane;
	}
	
	
	
	public HBox getVolumnPane() {
		HBox hBox = new HBox(60  * scale);
		
		Slider volumn = new Slider();
		volumn.setMax(1);
		this.volume = volumn;
		volumn.setPrefWidth(150  * scale);
		volumn.setPrefHeight(30  * scale);
		
		File file = new File("Settings.txt");
		
		try (Scanner scanner = new Scanner(file)) {
			scanner.nextLine();
			volumn.adjustValue(Double.parseDouble(scanner.nextLine()));
			BasicTools.volumeValue.setValue(volumn.getValue());
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
		volumn.valueProperty().addListener(e -> {
			BasicTools.volumeValue.setValue(volumn.getValue());
		});
	
		hBox.setPadding(new Insets(0, 100  * scale, 0 ,0));
		
		
		
		ImageView volumnImageView = new ImageView("pics/VolumnLabel.png");
		volumnImageView.setFitWidth(67 * 4  * scale);
		volumnImageView.setFitHeight(27 * 4  * scale);
		
		
		
		Label volumnLabel = new Label("", volumnImageView);
		
		hBox.setAlignment(Pos.CENTER_RIGHT);
		hBox.getChildren().add(volumnLabel);
		hBox.getChildren().add(volumn);
		
		return hBox;
	}
	
	
	public HBox getBackSavePane(Stage primaryStage) {
		HBox hBox = new HBox(150  * scale);
		hBox.setPadding(new Insets(10  * scale, 80  * scale, 0, 0) );
		hBox.setAlignment(Pos.BASELINE_RIGHT);
		hBox.getChildren().add(getBackLabel(primaryStage));
		hBox.getChildren().add(getSaveLabel(primaryStage));
		
		return hBox;
	}
	
	
	public HBox getBackSavePane(Stage primaryStage, Scene scene) {
		HBox hBox = new HBox(150  * scale);
		hBox.setPadding(new Insets(10  * scale, 80  * scale, 0, 0) );
		hBox.setAlignment(Pos.BASELINE_RIGHT);
		hBox.getChildren().add(getBackLabel(primaryStage, scene));
		hBox.getChildren().add(getSaveLabel(primaryStage, scene));
		return hBox;
	}
	
	
	public  Label getBackLabel(Stage primaryStage){
		
		ImageView imageView1 = new ImageView("pics/BackLabel1.png");
		ImageView imageView2 = new ImageView("pics/BackLabel2.png");
		
		imageView1.setFitWidth(37 * 5  * scale);
		imageView1.setFitHeight(13 * 5  * scale);
		imageView2.setFitWidth(37 * 5  * scale);
		imageView2.setFitHeight(13 * 5 * scale);
		
		Label backLabel  = new Label("", imageView1);
		backLabel.setOnMouseClicked(e -> {
			mouseClickAudio.play();
			BasicTools.changeSceneShrink(primaryStage, new MainMenu().getMainMenuScene(primaryStage));
		});
		
		backLabel.setOnMouseEntered(e ->{
			mouseEnterAudio.play();
			backLabel.setGraphic(imageView2);
		});
		
		backLabel.setOnMouseExited(e -> {
			backLabel.setGraphic(imageView1);
		});
		

		return backLabel;
	}
	
	
	public  Label getBackLabel(Stage primaryStage, Scene backscene){
		
		ImageView imageView1 = new ImageView("pics/BackLabel1.png");
		ImageView imageView2 = new ImageView("pics/BackLabel2.png");
		
		imageView1.setFitWidth(37 * 5 * scale);
		imageView1.setFitHeight(13 * 5 * scale);
		imageView2.setFitWidth(37 * 5 * scale);
		imageView2.setFitHeight(13 * 5 * scale);
		
		Label backLabel  = new Label("", imageView1);
		backLabel.setOnMouseClicked(e -> {
			mouseClickAudio.play();
			BasicTools.changeSceneShrink(primaryStage, backscene);
		});
		
		
		backLabel.setOnMouseEntered(e ->{
			mouseEnterAudio.play();
			backLabel.setGraphic(imageView2);
		});
		
		backLabel.setOnMouseExited(e -> {
			backLabel.setGraphic(imageView1);
		});
		
		return backLabel;
	}
	
	
	public  Label getSaveLabel(Stage primaryStage) {
		
		ImageView imageView1 = new ImageView("pics/SaveLabel1.png");
		ImageView imageView2 = new ImageView("pics/SaveLabel2.png");
		
		imageView1.setFitWidth(37 * 5 * scale);
		imageView1.setFitHeight(13 * 5 * scale);
		imageView2.setFitWidth(37 * 5 * scale);
		imageView2.setFitHeight(13 * 5 * scale);
		
		Label saveLabel = new Label("", imageView1);
		
		saveLabel.setOnMouseEntered(e ->{
			mouseEnterAudio.play();
			saveLabel.setGraphic(imageView2);
		});
		
		saveLabel.setOnMouseExited(e -> {
			saveLabel.setGraphic(imageView1);
		});
		
		
		File settingsFile = new File("Settings.txt");
		
		saveLabel.setOnMouseClicked(e ->{
			mouseClickAudio.play();
			try (PrintWriter printWriter = new PrintWriter(settingsFile)) {
				printWriter.println(windowSizeString);
				printWriter.println(BasicTools.volumeValue.getValue());
				
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
		
			if (isWindowSizeChanged) {
				BasicTools.changeStageSize(primaryStage);
				BasicTools.changeSceneExpand(primaryStage, new Settings().getSettingsScene(primaryStage));
				isWindowSizeChanged = false;
			}
			
			volume.adjustValue(BasicTools.volumeValue.getValue());
			
		});
		return saveLabel;

	}

	

	public  Label getSaveLabel(Stage primaryStage, Scene scene) {
		
		ImageView imageView1 = new ImageView("pics/SaveLabel1.png");
		ImageView imageView2 = new ImageView("pics/SaveLabel2.png");
		
		imageView1.setFitWidth(37 * 5 * scale);
		imageView1.setFitHeight(13 * 5 * scale);
		imageView2.setFitWidth(37 * 5 * scale);
		imageView2.setFitHeight(13 * 5 * scale);
		
		Label saveLabel = new Label("", imageView1);
		
		saveLabel.setOnMouseEntered(e ->{
			mouseEnterAudio.play();
			saveLabel.setGraphic(imageView2);
		});
		
		saveLabel.setOnMouseExited(e -> {
			saveLabel.setGraphic(imageView1);
		});
		
		
		
		File settingsFile = new File("Settings.txt");
		saveLabel.setOnMouseClicked(e ->{
			mouseClickAudio.play();
			try (PrintWriter printWriter = new PrintWriter(settingsFile)) {
				printWriter.println(windowSizeString);
				printWriter.println(BasicTools.volumeValue.getValue());
				
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
			if (isWindowSizeChanged) {
				BasicTools.changeStageSize(primaryStage);
				BasicTools.changeSceneExpand(primaryStage, new Settings().getSettingsScene(primaryStage, scene));
				isWindowSizeChanged = false;
			}
			
			volume.adjustValue(BasicTools.volumeValue.getValue());
			
		});
		return saveLabel;
	}
}
